package com.niit.service.impl;

import com.niit.entity.Order;
import com.niit.mapper.OrderMapper;
import com.niit.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;


/**
 * 订单服务实现类
 * @author fish66
 */
@Service
public class OrderServiceImpl implements IOrderService {

    private final RestTemplate restTemplate;

    public OrderServiceImpl(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 获取商品细节
     * @param productId 商品id
     * @return Product 对应于给定ID的商品详细信息。如果未找到匹配的商品，则可能返回null或抛出异常，具体行为取决于实现细节和所调用的服务的行为。
     */
    public Order getProductDetails(int productId) {
        String url = "http://localhost:8080/product/{id}";
        return restTemplate.getForObject(url, Order.class);
    }

    /**
     * 添加订单
     * @param order 订单信息
     * @return 结果
     */
    @Override
    public int addOrder(Order order) {
        return orderMapper.addOrder(order);
    }

    /**
     * 查询所有订单
     * @return 订单列表
     */
    @Override
    public List<Order> selectAllOrder() {
        return orderMapper.selectAllOrder();
    }

    /**
     * 根据订单id查询订单
     * @param orderId 订单id
     * @return 订单信息
     */
    @Override
    public Order selectOrderByOrderId(int orderId) {
        return orderMapper.selectOrderByOrderId(orderId);
    }

    /**
     * 根据收件人姓名查询订单
     * @param recipientName 收件人姓名
     * @return 订单列表
     */
    @Override
    public List<Order> selectOrderByRecipientName(String recipientName) {
        return orderMapper.selectOrderByRecipientName(recipientName);
    }

    /**
     * 根据订单id删除订单
     * @param orderId 订单id
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderId(int orderId) {
        return orderMapper.deleteOrderByOrderId(orderId);
    }

    /**
     * 根据订单id修改订单状态
     * @param orderId 订单id
     * @return 结果
     */
    @Override
    public int updateOrderByOrderId(int orderId) {
        return orderMapper.updateOrderByOrderId(orderId);
    }
}
